package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Outbound;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IOutboundService;
import com.igeek.collateral.vo.PageQueryVO;
import com.igeek.collateral.vo.ZInboundVo;
import com.igeek.collateral.vo.ZOutboundVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/8
 **/
@RestController
@RequestMapping("/admin/outbound")
@Api(tags = "出库接口")
@Slf4j
public class OutBoundController {
    @Autowired
    private IOutboundService iOutboundService;

    //查已出库的押品信息
    @GetMapping("findPageOut")
    @ApiOperation("条件分页查询已出库信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Outbound> page = iOutboundService.findPageOut(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //查询正常出库的信息条件
    @GetMapping("findPageZ")
    @ApiOperation("条件分页查询正常出库信息")
    public Result findPageZ(PageQueryVO vo){
        try {
            Page<Outbound> page = iOutboundService.findPageZ(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //查询置换出库的信息条件
    @GetMapping("findPageR")
    @ApiOperation("条件分页查询置换出库信息")
    public Result findPageR(PageQueryVO vo){
        try {
            Page<Outbound> page = iOutboundService.findPageR(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }

    //查询临时出库的信息条件
    @GetMapping("findPageT")
    @ApiOperation("条件分页查询临时出库信息")
    public Result findPageT(PageQueryVO vo){
        try {
            Page<Outbound> page = iOutboundService.findPageT(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }






    //通过id查询出库信息
    @GetMapping("/{id}")
    @ApiOperation("通过ID查询出库信息")
    public Result findById(@PathVariable("id") Integer id){
        Outbound outbound = iOutboundService.getById(id);
        if(outbound!=null) {
            return new Result(true, "查询信息成功", outbound);
        }
        return new Result(false , "查询信息失败");
    }



    //新增出库记录
    @PostMapping("add")
    @ApiOperation("添加出库记录")
    public Result add(@RequestBody Outbound outbound,@RequestParam("collateralId") Integer collateralId)
    {
        try {
            boolean save = iOutboundService.add(outbound,collateralId);
            if(save)
            {
                return new Result(true,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");

    }


    //通过前端点击同意，将该出库的状态改为已出库
    @PutMapping("update/status")
    @ApiOperation("修改状态信息")
    public Result updateS(@RequestBody Outbound outbound )
    {
        boolean b = iOutboundService.updateById(outbound);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");

    }


    //通过id更新对应的信息（置换出库）
    @PutMapping("updateS/RcollateralId")
    @ApiOperation("通过ID修改入库信息")
    public Result updateRS(@RequestBody Outbound outbound ,@RequestParam("collateralId") Integer collateralId)
    {
        boolean b = iOutboundService.updateRS(outbound,collateralId);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");

    }



    //通过id修改该出库的状态为未出库(否决的)
    @PutMapping("updateReplaceS")
    @ApiOperation("将出库状态修改为未出库")
    public Result updateById(@RequestBody Outbound outbound ) {
        boolean b=   iOutboundService.updateById(outbound);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");
    }


    //正常进库修改部分信息
    @PutMapping("updatePart")
    @ApiOperation("修改进库信息的部分信息")
    public Result updatePart(@RequestBody ZOutboundVo vo, @RequestParam("outboundId") Integer outboundId)
    {
        boolean b=iOutboundService.updatePart(vo,outboundId);
        if(b)
        {
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");
    }


    //删除操作，根据进库id删除
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除进库信息")
    public Result delete (@PathVariable("id") Long id)
    {
        try {
            boolean flag=iOutboundService.delete(id);
            if(flag)
            {
                return new Result(true,"删除成功");
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }



    //通过押品id找到对应的出库类型
    @GetMapping("check")
    @ApiOperation("查找对应的出库类型")
    public Result checkType(@RequestParam("collateralId") Integer collateralId)
    {
        int a=iOutboundService.check(collateralId);
        if(!ObjectUtils.isEmpty(a)){
            return new Result(true,"查找成功",a);
        }
        return new Result(false,"查找失败");
    }


    //根据押品id删除出库信息
    @DeleteMapping("deleteByCid")
    @ApiOperation("根据押品id删除出库信息")
    public Result deleteMessByCid(@RequestParam("collateralId") Integer collId)
    {
        boolean b=iOutboundService.deleteMessByCid(collId);
        if(b)
        {
            return new Result(true,"删除对应信息成功");
        }
        return new Result(false,"删除对应信息失败");
    }


}
